/*
** my_str_to_wordtab.c for  in /home/gomes_m//my-project-a-moi/server
** 
** Made by mickael gomes
** Login   <gomes_m@epitech.net>
** 
** Started on  Fri Jul 26 14:01:18 2013 mickael gomes
** Last update Fri Jul 26 14:14:11 2013 mickael gomes
*/

#include		<stdlib.h>
#include		"struct.h"
#include		"prototypes.h"

static int		get_word_size(char *str, int *pos)
{
  int			i = 0;

  while (str[i + *pos] && str[i + *pos] != ' ')
    i++;
  return (i);
}

static char		*get_the_word(char *str, int *pos)
{
  char			*word;
  int			i = 0;

  word = xmalloc(get_word_size(str, pos) + 1);
  while (str[*pos] && str[*pos] != ' ')
    word[i++] = str[(*pos)++];
  word[i] = 0;
  while (str[*pos] && str[*pos] <= ' ')
    (*pos)++;
  return (word);
}

static int		get_word_nb(char *str)
{
  int			i = 0;
  int			word_count = 0;

  while (str[i])
    {
      word_count++;
      while (str[i] && str[i++] != ' ');
      while (str[i] <= ' ' && str[i++]);
    }
  return (word_count);
}

char			**my_str_to_wordtab(char *str)
{
  char			**tab;
  int			i = 0;
  int			pos = 0;
  int			word_count;

  while (*str <= ' ')
    str++;
  word_count = get_word_nb(str);
  tab = xmalloc((word_count + 1) * sizeof(char*));
  while (i < word_count)
    tab[i++] = get_the_word(str, &pos);
  tab[i] = NULL;
  return (tab);
}
